package com.sport.setting.service.impl;

import com.sport.apis.annotation.DIAccountUserBo;
import com.sport.model.common.dtos.ResponseResult;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.setting.dtos.*;
import com.sport.model.setting.bos.SportEventsGamesBo;
import com.sport.model.user.bos.AccountUserBo;
import com.sport.setting.repository.SportEventsGameGradeRepository;
import com.sport.setting.repository.SportMeetingDefineItemRepository;


import com.sport.setting.repository.SportMemberEnrollRepository;
import com.sport.setting.service.SportMeetingDefineItemService;
import com.sport.utils.common.BaseUtils;
import com.sport.utils.common.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class SportMeetingDefineItemServiceImpl implements SportMeetingDefineItemService {
    @Autowired
    SportMeetingDefineItemRepository sportMeetingDefineItemRepository;

    @Autowired
    SportMemberEnrollRepository sportMemberEnrollRepository;

    @Resource
    SportEventsGameGradeRepository sportEventsGameGradeRepository;

    @Override
    @DIAccountUserBo
    public ResponseResult defineItem(SportEventsGamesDto sportEventsGamesDto) {
        if (sportEventsGamesDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        return sportMeetingDefineItemRepository.defineItem(sportEventsGamesDto);
    }

    /**
     * 查询比赛组,以及每个年级对应单位
     * @return
     */
    @Override
    public ResponseResult showGradeAndUnit() {
        Map<String, List<String>> map = sportMemberEnrollRepository.showGradeAndUnit();

        return  ResponseResult.okResult(map);
    }

    /**
     * 显示每个比赛组或每个单位有多少人
     * @param sportNumberShowDto
     * @return
     */
    @Override
    @DIAccountUserBo
    public ResponseResult showQuantity(SportNumberShowDto sportNumberShowDto) {
        if(sportNumberShowDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        return sportMemberEnrollRepository.showQuantity(sportNumberShowDto);
    }

    @Override
    @DIAccountUserBo
    public ResponseResult assignedNumber(SportNumberParamDto sportNumberParamDto) {
        return sportMemberEnrollRepository.assignedNumber(sportNumberParamDto);
    }

    @Override
    public ResponseResult showEventsGame() {

        List<SportEventsGamesBo> eventsGames = sportMeetingDefineItemRepository.getEventsGames(AccountUserBo.getUser().getSchoolTenementId());

        if (eventsGames == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        return ResponseResult.okResult(eventsGames);
    }

    @Override
    @DIAccountUserBo
    public ResponseResult setEventsGameForGrade(List<SportEventsGamesChooseDto> sportEventsGamesChooseDtos) {

        ThreadPoolUtils.putOperationToThread(()-> sportEventsGameGradeRepository.setEventsGameForGrade(sportEventsGamesChooseDtos));

        return ResponseResult.okResult("设置成功！");
    }

    @Override
    public ResponseResult deleteItemProject(List<Long> deleteIds) {
        return sportMeetingDefineItemRepository.deleteItemProject(deleteIds, AccountUserBo.getUser().getSchoolTenementId()) ? ResponseResult.okResult("成功删除")
                : ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "部分项目为公有项目，不可删除！");
    }

    @Override
    @DIAccountUserBo
    public ResponseResult setTempGameForSchool(SportEventsTempGameDto sportEventsTempGameDto) {
        List<Long> tempGameIds = sportMeetingDefineItemRepository.getTempGameIds(sportEventsTempGameDto.getAccountUserBo().getSchoolTenementId(), sportEventsTempGameDto.getAccountUserBo().getSportId());
        if (tempGameIds == null) {
            sportMeetingDefineItemRepository.insertTempGame(sportEventsTempGameDto);
            return ResponseResult.okResult("操作成功！");
        }

        List<Long> addProject = BaseUtils.findListsDifferentElement(tempGameIds, sportEventsTempGameDto.getIds());

        List<Long> delProject = BaseUtils.findListsDifferentElement(sportEventsTempGameDto.getIds(), tempGameIds);

        ThreadPoolUtils.putOperationToThread(()->{
            SportEventsTempGameDto copy = BaseUtils.copyProperties(sportEventsTempGameDto, SportEventsTempGameDto.class);
            copy.setTarget(addProject);
            sportMeetingDefineItemRepository.insertTempGame(copy);
        });

        ThreadPoolUtils.putOperationToThread(()->{
            SportEventsTempGameDto copy = BaseUtils.copyProperties(sportEventsTempGameDto, SportEventsTempGameDto.class);
            copy.setTarget(delProject);
            sportMeetingDefineItemRepository.insertTempGame(copy);
        });

        return ResponseResult.okResult("操作成功！请耐心等待！");
    }

    @Override
    public ResponseResult getTempGameForSchool() {
        List<SportEventsGamesBo> tempGameForSchool = sportMeetingDefineItemRepository.getTempGameForSchool(AccountUserBo.getUser().getSchoolTenementId(), AccountUserBo.getUser().getSportId());
        if (tempGameForSchool == null)
            return ResponseResult.okResult("暂时还没有设置比赛项目！");

        return ResponseResult.okResult(tempGameForSchool);
    }
}
